<template>
    <Transition v-show="modelValue">
        <div :style="{
            backgroundColor: backgroundColor,
        }" class="Dialog">
            <div class="contain">
                <div class="slot">
                    <slot></slot>
                </div>
                <footer>
                    <div class="cancel" :style="{
                        color: cancaelColor,
                        backgroundColor: cancaelBgColor,
                    }" @click="onCancel">
                        <span>
                            {{ cancel }}
                        </span>
                    </div>
                    <div class="confirm" :style="{
                        color: confirmColor,
                        backgroundColor: confirmBgColor,
                    }" @click="onConfirm">
                        <div class="loading-box" v-if="loading">
                            <van-loading type="spinner"></van-loading>
                        </div>
                        <span v-else>
                            {{ confirm }}
                        </span>
                    </div>
                </footer>
            </div>
        </div>
    </Transition>
    <van-overlay z-index="100" :show="modelValue" @click="onClickMask" />
</template>


<script setup>
import { reactive, watch } from 'vue'
const props = defineProps({
    modelValue: {
        type: Boolean,
        required: true,
        default: false
    },
    backgroundColor: {
        type: String,
        required: false,
        default: '#fff'
    },
    confirm: {
        type: String,
        required: true,
    },
    cancel: {
        type: String,
        required: true,
    },
    confirmColor: {
        type: String,
        required: false,
        default: '#fff'
    },
    cancaelColor: {
        type: String,
        required: false,
        default: '#fff'
    },
    confirmBgColor: {
        type: String,
        required: false,
        default: '#1690F8'
    },
    cancaelBgColor: {
        type: String,
        required: false,
        default: '#1690F8'
    },
    clickMaskIsClose: {
        type: Boolean,
        required: false,
        default: true
    },
    loading: {
        type: Boolean,
        required: false,
        default: false
    }
})
const emit = defineEmits(['update:modelValue', 'onCancel', 'onConfirm', 'onClose'])


function onCancel(params) {
    emit('update:modelValue', false)
    emit('onCancel', params)
}

function onConfirm(params) {
    emit('update:modelValue', false)
    emit('onConfirm', params)
}

function onClickMask() {
    if (props.clickMaskIsClose) {
        emit('update:modelValue', false)
    } else {
        return
    }
}

watch(() => props.modelValue, (newVal, oldVal) => {
    if (!newVal) {
        emit('onClose',)
    }
})
</script>

<style lang="less" scoped>
.v-enter-active,
.v-leave-active {
    transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
    opacity: 0;
}

.Dialog {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    width: 80%;
    min-height: 300px;
    z-index: 1000;

    .contain {
        min-height: 300px;
        height: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        .slot {
            width: 80%;
            box-sizing: border-box;
        }

        footer {
            margin-top: auto;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            justify-content: center;

            .confirm,
            .cancel {
                border-radius: 5px;
                padding: 10px 50px;
            }

            .confirm {
                margin-left: 5px;
            }

            .cancel {
                margin-right: 5px;
            }
        }
    }
}
</style>